package com.teradata.webmvc.controller;

import net.bytebuddy.utility.RandomString;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "wait", urlPatterns = "/wait/*")
public class WaitServlet extends HttpServlet {

    private final Object xxx = new Object();

    @Override
    public void service(HttpServletRequest req,final HttpServletResponse resp) throws ServletException, IOException {


        Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Thread.sleep(5000);
                    resp.getWriter().write("hello" + RandomString.make());

                    synchronized (xxx){
                        xxx.notifyAll();
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        });


        thread.start();

        synchronized (xxx) {
            try {
                xxx.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }



    }
}
